package com.ceres.device.web.service.impl;

import com.ceres.device.pojo.CustomerDevice;
import com.ceres.device.pojo.Device;
import com.ceres.device.pojo.OrderDevice;
import com.ceres.device.web.dao.CustomerDeviceMapper;
import com.ceres.device.web.dao.DeviceWebMapper;
import com.ceres.device.web.dao.OrderDeviceMapper;
import com.ceres.device.web.service.OrderDeviceService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class OrderDeviceServiceImpl implements OrderDeviceService {

    @Autowired
    private OrderDeviceMapper orderDeviceMapper;

    @Autowired
    private DeviceWebMapper deviceWebMapper;

    @Autowired
    private CustomerDeviceMapper customerDeviceMapper;

    /**
     * 根据用户id查询用户订单
     *
     * @param cid
     * @return
     */
    @Override
    public List<OrderDevice> findByCustomerId(Integer cid) {
        List<OrderDevice> orderDeviceList = orderDeviceMapper.findByCustomerId(cid);
        return orderDeviceList;
    }


    /**
     * 修改订单状态
     *
     * @param logoNum
     * @return
     */
    @Override
    public int updateOrder(Integer id, Integer logoNum) {
        int count = orderDeviceMapper.updateOrder(id);
        return count;
    }


    /**
     * 官方采购
     * @return
     */
    @Override
    public void add(OrderDevice orderDevice) {
        //添加到订单表
        orderDeviceMapper.addOrder(orderDevice);

        Integer num = orderDevice.getNum();

        //获取设备id
        Integer did = orderDevice.getDid();
        //农业组织ID
        Integer cid = orderDevice.getCid();
        //根据设备id获取设备数据
        Device device = deviceWebMapper.findById(did);
        //编码
        String code = device.getCode();
        //设备名称
        String name = device.getName();
        //设备类型
        String type = device.getType();
        //设备品牌
        String brand = device.getBrand();
        //设备型号
        String model = device.getModel();

        //创建一个用户设备库对象
        //根据数量循环添加
        for(int i=0;i<num;i++){
            CustomerDevice customerDevice=new CustomerDevice(null,did,cid,code,name,type,brand,model,"0",null);
            customerDeviceMapper.insert(customerDevice);
        }
    }


    /**
     * 分页条件查询
     *
     * @param searchMap
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Page<OrderDevice> findPage(Map searchMap,Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Example example = createExample(searchMap);
        Page<OrderDevice> orderDevicePage = (Page<OrderDevice>) orderDeviceMapper.selectByExample(example);
        return orderDevicePage;
    }


    /**
     * 构建查询对象
     *
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(OrderDevice.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            //订单di
            if (searchMap.get("id") != null && !"".equals(searchMap.get("id"))) {
                criteria.andEqualTo("id", searchMap.get("id"));
            }
            //设备id
            if (searchMap.get("did") != null && !"".equals(searchMap.get("did"))) {
                criteria.andEqualTo("did", searchMap.get("did"));
            }
            //农业组织id
            if (searchMap.get("cid") != null && !"".equals(searchMap.get("cid"))) {
                criteria.andEqualTo("cid", searchMap.get("cid"));
            }
            //数量
            if (searchMap.get("num") != null) {
                criteria.andEqualTo("num", searchMap.get("num"));
            }
            //价格（分）
            if (searchMap.get("money") != null) {
                criteria.andEqualTo("money", searchMap.get("money"));
            }
            // 订单状态 0未送达，1已送达
            if (searchMap.get("orderStatus") != null && !"".equals(searchMap.get("orderStatus"))) {
                criteria.andEqualTo("orderStatus", searchMap.get("orderStatus"));
            }
        }
        return example;
    }
}
